﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZX.Web.UI.Controls;
using ZX.EHR.Library;

namespace ZX.WebApp.EHR
{
    public partial class calendarData : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string action = this.GetStringFromQueryString("action", "");//this.Request["action"];   
            string json = "";
            string solutionId = this.Request.Params["SolutionID"];
            if (action == string.Empty || solutionId == string.Empty) return;
            wkCalendar instance = new wkCalendar();
            ZX.BLL.Extend.BaseBussnies bll = null;
            string condtion = string.Empty;
            switch (action)
            {
                case "quickAdd":
                    //快速增加日程
                    try
                    {
                        string holiday = this.Request.Params["Holiday"];
                        string dataType = this.Request.Params["DayType"];
                        if (holiday != null && dataType != null && solutionId!=null)
                        {
                            //判断该日期是否存在
                            condtion = string.Format("SolutionID='"+solutionId+"' and Holiday like '{0}%'", DateTime.Parse(holiday).ToString("yyyy-MM-dd"));
                            bll = new ZX.BLL.Extend.BaseBussnies(instance);
                            instance = bll.GetEntityByCondition(condtion) as wkCalendar;
                            if (instance != null)//该日期数据存在
                            {
                                if (dataType == "3") //正常上班
                                {
                                    //删除该日期数据 
                                    bll.delete(instance);
                                }
                                else//更新数据
                                {
                                    instance.DayType = dataType;
                                    bll.Update(instance);
                                }
                                json = "{\"Data\":null,\"IsSuccess\":true,\"Msg\":\"操作成功!\"}";
                            }
                            else//该日期数据不存在
                            {
                                if (dataType != "3")//为假期才插入数据
                                {
                                    instance = new wkCalendar();
                                    instance.ID = Guid.NewGuid();
                                    instance.Holiday = DateTime.Parse(holiday);
                                    instance.SolutionID = new Guid(solutionId);
                                    instance.DayType = dataType;
                                    instance.AddUser = this.User.UserID;
                                    instance.AddTime = DateTime.Now;
                                    bll.Insert(instance);
                                    string json2 = ZX.Common.Globals.ObjectToJSON(instance);
                                    json = "{\"Data\":" + json2 + ",\"IsSuccess\":true,\"Msg\":\"操作成功!\"}";
                                }
                                else
                                {
                                    json = "{\"Data\":null,\"IsSuccess\":true,\"Msg\":\"操作成功!\"}";
                                }
                            }              
                        }
                        else
                        {
                            json = "{\"Data\":null,\"IsSuccess\":false,\"Msg\":\"传入的参数有误!\"}";
                        }
                    }
                    catch
                    {
                        json = "{\"Data\":null,\"IsSuccess\":false,\"Msg\":\"操作失败!\"}";
                    }
                    break;
                case "add":
                    //向数据库添加日程记录
                    break;
                case "edit":
                    //修改日程记录
                    break;
                case "del":
                    //删除日程记录
                    break;
                case "getAll":
                    //获取所有日历数据
                    DateTime showdate =DateTime.Now;
                    try
                    {
                        showdate = DateTime.Parse(this.Request.Params["showdate"]);
                    }
                    catch
                    { }
                    IList<wkCalendar> list = null;
                    var format = new ScheduleViewFormat(CalendarViewType.month, showdate, DayOfWeek.Monday);
                    condtion = string.Format("SolutionID='" + solutionId + "' and Holiday between '{0}' and '{1}'", format.StartDate, format.EndDate);
                    bll = new ZX.BLL.Extend.BaseBussnies(instance);
                    list = bll.GetEntitys<wkCalendar>(condtion);
                    if (list == null || list.Count == 0)
                    {
                        json = "[]";
                    }
                    else
                    {
                        json = ZX.Common.Globals.getJSON<wkCalendar>(list);
                        json = "[" + json + "]";
                    }
                    //json = "{\"start\":\"Mon Aug 27 00:00:00 UTC+0800 2012\",\"end\":\"Sun Sep 30 00:00:00 UTC+0800 2012\",\"issort\":true,\"error\":null,\"events\":[1,\"121212\",\"Mon Aug 27 00:00:00 UTC+0800 2012\",\"Mon Aug 27 23:59:59 UTC+0800 2012\",1,0,0,,-1,1,\"12121\",\"\"]}";
                    break;
            }
            this.Response.ContentType = "application/json";
            this.Response.Write(json);
            this.Response.End();
        }
    }
}